﻿Zepto(function ($) {
    FastClick.attach(document.body);
    var aid = $("#aid").val()
    var searchInput = $('#searchInput'),
        productList = $('#productList'),
        loadingTxt = $('.txt-loading');


    var page = 0, pagesize = 20, load = 1, sort = 0;

    var _w = $('.wrapper').width(), picH = (_w - 25) / 2;
    if (_w > 500) {
        picH = (_w - 10 - 5 - 5 - 10) / 3;
    }
    var keyword = getQueryString("Key");
    if (keyword != null && keyword != "") {

        $(".tagbox").hide();
        searchInput.val(keyword);
    }

    searchInput.focus();
    var channel = 35;
    var listStyle = 1;
    $('.sort-option-area li').on('click', function () {
        var _this = $(this);
        if (!_this.hasClass('active')) {
            $('.sort-option-area li.active').removeClass('active');
            _this.addClass('active');

            sort = _this.data('type');
            page = 0;
            load = 1;

            productList.html('');
            $('body').scrollTop(0);
            loadingTxt.html('努力获取中...');
            getProductList();
        }
    });
    searchInput.on('keypress', function (e) {
        var _this = $(this);
        if (e.keyCode == 13 && $.trim(_this.val()) != '') {
            window.location.href = "/" + aid + "/search.html?Key=" + _this.val();
            //$(".tagbox").hide();
            //productList.html('');
            //load = 1;
            //page = 0;
            //getProductList();
        }
    });

    getProductList();

    /* scroll event start */
    var de = document.documentElement;
    $(document).on('scroll', function () {
        var sH = de.scrollHeight, cH = de.clientHeight;
        if (sH - cH - $('body').scrollTop() <= 400) {
            getProductList();
        }
    });
    /* scroll event end */

    function getProductList() {
        if (!load) {
            return;
        }
        var q = searchInput.val();
        if (!q || !q.trim()) {
            return;
        }
        $.ajax({
            url: '/' + aid + '/getZhekouListByCate.html',
            type: 'Post',
            dataType: 'json',
            data: {
                'limit': page,
                'pagesize': pagesize,
                'cid': 0,
                'sort': sort,
                'Key': q
            },
            beforeSend: function () {
                load = 0;
                loadingTxt.html('努力加载中...');
            },
            success: function (data) {
                if (data.rows.CurrentPageList != null) {
                    createProductList(data.rows.CurrentPageList);
                    if (data.rows.CurrentPageList.length >= pagesize) {
                        $(".sort-option-area").show();
                        page++;
                        load = 1;
                    } else {
                        loadingTxt.html('没有更多关于'+q+'的商品了');
                    }
                }

            }
        });
    }

    function createProductList(pl) {
        var htmlstr = '';
        for (var i = 0, len = pl.length; i < len; i++) {
            var zk = pl[i];
            htmlstr += '<a href="/' + aid + '/goods/' + zk.auctionId + '.html">';
            htmlstr += '<div class="product-item-t2" style="width:' + picH + 'px;">';
            htmlstr += '<div class="pic lazy new" data-original="' + zk.pictUrl + '_400x400" style="width: ' + picH + 'px; height: ' + picH + 'px;"></div>';
            htmlstr += '<p class="title">';
            //if (zk.post_free == 1) {
            htmlstr += '<span class="post-free">包邮</span>&nbsp;';
            // }
            htmlstr += zk.title + '</p>';

            htmlstr += '<div class="progress-area">';
            htmlstr += '<p class="pro-text">已领' + Math.floor(zk.couponTotalCount * 100) + '%</p>';
            htmlstr += '<div class="pro-container">';
            htmlstr += '<div data-pro="' + zk.progress + '" class="pro" style="width:' + Math.floor(zk.couponTotalCount * 100) + '%;"></div>';
            htmlstr += '</div>';
            htmlstr += '</div>';

            htmlstr += '<div class="price-area">';
            htmlstr += '<p class="gap-price"><em class="number-font">' + zk.couponAmount + '</em>元券</p>';
            htmlstr += '<p class="price">券后价&yen;<em class="number-font">' + (zk.zkPrice - zk.couponAmount).toFixed(1) + '</em></p>';
            htmlstr += '</div>';
            htmlstr += '</div>';
            htmlstr += '</a>';
        }
        productList.append(htmlstr);
        Util.lazyLoad('lazy.new');
        $('.lazy.new').removeClass('new');
    }
});
// JavaScript Document
(function (doc, win) {
    var docEl = doc.documentElement;
    resizeEvt = "orientationchange" in window ? "orientationchange" : "resize",
    recalc = function () {
        var clientWidth = docEl.clientWidth;
        if (!clientWidth) {
            return
        }
        if (clientWidth > 640) {
            clientWidth = 640
        }
        docEl.style.fontSize = (clientWidth / 10) + "px";
    };
    if (!doc.addEventListener) {
        return
    }
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener("DOMContentLoaded", recalc, false)
})(document, window);
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = (window.location.search.substr(1).match(reg));

    if (r != null) return decodeURI(r[2]);
    return null;


}